package com.bike.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhBank;
import com.bike.entity.HhBankBranch;
import com.bike.entity.HhCity;
import com.bike.mapper.HhBankBranchMapper;
import com.bike.mapper.HhBankMapper;
import com.bike.mapper.HhCityMapper;
import com.bike.util.PageData;
import com.bike.util.TransFerUtil;
import com.bike.util.UuidUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhBankBranchServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhBankBranchMapper hhBankBranchMapper;
	@Autowired
	HhBankMapper hhBankMapper;
	@Autowired
	HhCityMapper cityMapper;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, HhBankBranch record) {
		String parentId=pd.getString("parentId");
		HhBank bank=hhBankMapper.selectByPrimaryKey(parentId);
		record.setParentId(parentId);
		record.setParentName(bank.getName());
		record.setParentCode(bank.getBankCode());
		HhCity city=cityMapper.selectByPrimaryKey(pd.getLong("cityparentid"));
		if(city.getLevel()==1){
			 record.setProvince(city.getTitle());
			 record.setProvinceId(city.getItemid());
		}else if(city.getLevel()==2){
			 record.setCity(city.getTitle());
			 record.setCityId(city.getItemid());
			 record.setProvince(city.getParentName());
			 record.setProvinceId(city.getParentid());
		}else{
			 record.setCity(city.getParentName());
			 record.setCityId(city.getParentid());
			 record.setProvince(city.getRootName());
			 record.setProvinceId(city.getRootid());
		}
		
		if(pd.getString("bankBranchId").equals("")){
			record.setBankBranchId(UuidUtil.get32UUID());
			hhBankBranchMapper.insertSelective(record);
		}else{
			//编辑
			hhBankBranchMapper.updateByPrimaryKeySelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhBankBranchById(String bankBranchId) {
		return hhBankBranchMapper.deleteByPrimaryKey(bankBranchId);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhBankBranchList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
	 
		Long cityId=pd.getLong("cityparentid");
		if(cityId>0){
			HhCity city=cityMapper.selectByPrimaryKey(cityId);
			if(city.getLevel()==1){
				pd.put("provinceId", city.getItemid());
			}else{
				pd.put("cityId", city.getItemid());
			}
		}
		
		List<HhBankBranch> records= hhBankBranchMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhBankBranch>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhBankBranch> listAll(PageData pd) {
		return  hhBankBranchMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhBankBranch selectByHhBankBranchId(String bankBranchId) {
		return hhBankBranchMapper.selectByPrimaryKey(bankBranchId);
	}
}

